package com.oedtech.esl.handler;

import com.oedtech.esl.json.AppJsonResult;
import com.oedtech.esl.json.SNJSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;

/**
 * 全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    public static Logger LOGGER= LoggerFactory.getLogger(GlobalExceptionHandler.class);
    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public SNJSONObject HttpMessageNotReadableException(Exception e){
        return new SNJSONObject("2","缺少必要参数",null);

    }

    @ExceptionHandler(ServletException.class)
    @ResponseBody
    public SNJSONObject HttpRequestMethodNotSupportedException(Exception e){
        LOGGER.warn(e.getMessage());
        return new SNJSONObject("2",e.getMessage(),null);

    }
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public SNJSONObject Exception(Exception e){
//        if(e instanceof HttpMessageNotReadableException){
//            return new AppJsonResult(0,"error","缺少必要参数");
//        }
        LOGGER.error("GlobalException:",e);
        return new SNJSONObject("2",e.getMessage(),null);

    }

}
